package com.forum.module.blog.controller.comment;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.blog.controller.comment.vo.CommentPageReqVO;
import com.forum.module.blog.controller.comment.vo.CommentRespVO;
import com.forum.module.blog.controller.comment.vo.CommentSavaReqVO;
import com.forum.module.blog.convert.comment.CommentConvert;
import com.forum.module.blog.convert.user.UserConvert;
import com.forum.module.blog.dal.dataobject.comment.CommentDO;
import com.forum.module.blog.dal.dataobject.user.UserDO;
import com.forum.module.blog.service.comment.CommentService;
import com.forum.module.blog.service.user.UserService;
import com.forum.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/4 19:17
 */
@Tag(name = "博客管理 - 评论")
@RestController
@RequestMapping("/blog/comment")
public class CommentController {
    private final CommentService commentService;

    private final UserService userService;

    public CommentController(CommentService commentService,
                             UserService userService) {
        this.commentService = commentService;
        this.userService = userService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增评论")
    public DataResult<Long> createComment(@RequestBody CommentSavaReqVO reqVO) {
        Long id = commentService.createComment(reqVO);
        return success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新评论")
    public DataResult<Boolean> updateComment(@RequestBody CommentSavaReqVO reqVO) {
        commentService.updateComment(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<Boolean> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteById(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询评论列表")
    public DataResult<PageResult<CommentRespVO>> page(@RequestBody @Valid CommentPageReqVO pageReqVO) {
        // 获得文章分页列表
        pageReqVO.setParentId(0L);
        PageResult<CommentDO> pageResult = commentService.getCommentPage(pageReqVO);
        long total = commentService.count(new LambdaUpdateWrapper<CommentDO>().eq(CommentDO::getArticleId, pageReqVO.getArticleId()));
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

//        List<CommentDO> commentDOS = pageResult.getList();
//        List<CommentRespVO> commentRespVOS = commentDOS.stream().map(commentDO -> {
//            CommentRespVO commentRespVO = CommentConvert.INSTANCE.covert(commentDO);
//            List<CommentDO> childComments = commentService.getChildComments(commentDO.getId());
//            commentRespVO.setChildren(childComments.stream().map(CommentConvert.INSTANCE::covert).collect(Collectors.toList()));
//            return commentRespVO;
//        }).collect(Collectors.toList());
        List<UserDO> userDOS = userService.list();
        Map<Long, UserDO> userDOMaps = userDOS.stream().collect(Collectors.toMap(UserDO::getId, userDO -> userDO));
        List<CommentRespVO> commentRespVOS = pageResult.getList().stream()
                .map(commentDO -> {
                    CommentRespVO respVO = CommentConvert.INSTANCE.covert(commentDO);
                    populateChildren(respVO, 1, userDOMaps);
                    respVO.setUserInfo(UserConvert.INSTANCE.covert(userDOMaps.get(respVO.getUserId())));
                    return respVO;
                }).collect(Collectors.toList());

        return success(new PageResult<>(commentRespVOS, total));
    }

    private void populateChildren(CommentRespVO commentRespVO, int currentDepth, Map<Long, UserDO> userDOMaps) {
        if (currentDepth > 8) {
            return;
        }
        List<CommentDO> childComments = commentService.getChildComments(commentRespVO.getId());
        List<CommentRespVO> childRespVO = childComments.stream()
                .map(CommentConvert.INSTANCE::covert)
                .collect(Collectors.toList());
        commentRespVO.setChildren(childRespVO);
        commentRespVO.setUserInfo(UserConvert.INSTANCE.covert(userDOMaps.get(commentRespVO.getUserId())));
        childRespVO.forEach(child -> populateChildren(child, currentDepth + 1, userDOMaps));
    }
}
